<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author LYS
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="false"
    :okAuth="'orther:qus:pmQusrequest:edit'"
    @register="registerDrawer"
    @ok="handleSubmit('4')"
    width="60%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="pr-1 m-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm">
    <template #msgContent="{ model, field }">
      <!-- <WangEditor
        v-model:value="model[field]"
        :bizKey="record.id"
        :bizType="'pmQusrequest_' + field"
        :height="300"
        :disabled="true"
      /> -->
      <div v-html="model[field]"></div>
    </template>
    </BasicForm>


    <!-- <template #centerFooter>
      <a-button
        class="mt-2"
        @click="handleSubmit('9')"
        v-auth="'orther:qus:pmQusrequest:edit'"
      >
        <Icon icon="ant-design:save-outlined" /> {{ t('草稿') }}
      </a-button>
    </template> -->
  </BasicDrawer>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsOrtherQusPmQusrequestForm',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { WangEditor } from '/@/components/WangEditor';
  import { PmQusrequest, pmQusrequestSave, pmQusrequestForm } from '/@/api/orther/qus/pmQusrequest';

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('orther.qus.pmQusrequest');
  const { showMessage } = useMessage();
  const record = ref<PmQusrequest>({} as PmQusrequest);
  const getTitle = computed(() => ({
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: t('查看数据变更申请'),
  }));

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('紧急程度'),
      field: 'level',
      component: 'Select',
      componentProps: {
        dictType: 'zf_qus_level',
        allowClear: true,
      },
      itemProps: { validateTrigger: 'blur' },
      dynamicDisabled: true,
    },
    {
      label: t('问题描述'),
      field: 'content',
      component: 'Input',
      componentProps: {
        maxlength: 4000,
      },
      slot: 'msgContent',
      colProps: { lg: 24, md: 24 },
      dynamicDisabled: true,
    },
    {
      label: t('问题反馈'),
      field: 'remarks',
      component: 'InputTextArea',
      componentProps: {
        maxlength: 4000,
        rows: 5
      },
      colProps: { lg: 24, md: 24 },
      dynamicDisabled: true,
    },
    {
      label: t('处理结果'),
      field: 'result',
      component: 'Select',
      componentProps: {
        dictType: 'zf_qus_result',
        allowClear: true,
      },
      itemProps: { validateTrigger: 'blur' },
      dynamicDisabled: true,
    },
    {
      label: t('计划处理日期'),
      field: 'planDate',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD HH:mm',
        showTime: { format: 'HH:mm' },
      },
      dynamicDisabled: true,
    },
    {
      label: t('实际处理日期'),
      field: 'handDate',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD HH:mm',
        showTime: { format: 'HH:mm' },
      },
      dynamicDisabled: true,
    },
  ];

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { lg: 24, md: 24 },
  });

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    const res = await pmQusrequestForm(data);
    record.value = (res.pmQusrequest || {}) as PmQusrequest;
    record.value.__t = new Date().getTime();
    setFieldsValue(record.value);
    setDrawerProps({ loading: false });
  });

  async function handleSubmit(status) {
    try {
      const data = await validate();
      data.status=status
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        id: record.value.id,
      };
      // console.log('submit', params, data, record);
      const res = await pmQusrequestSave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('您填写的信息有误，请根据提示修正。'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>
